package org.jader.bolao.gui.swing.table;

import java.awt.Component;
import java.text.ParseException;

import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;
import javax.swing.text.NumberFormatter;

public class PlayerPointsCellEditor extends DefaultCellEditor {
	private static final long serialVersionUID = 1L;
	
	public PlayerPointsCellEditor() {
		super(new JFormattedTextField(new NumberFormatter()));
		this.setClickCountToStart(2);
	}
	
	@Override
	public Object getCellEditorValue() {
		JFormattedTextField inputComponent = (JFormattedTextField) this.getComponent();
		try {
			inputComponent.commitEdit();
		} catch (ParseException e) {}
		Object value = inputComponent.getValue();
		return Integer.parseInt(""+value);
	}

	@Override
	public Component getTableCellEditorComponent(JTable arg0, Object arg1,
			boolean arg2, int arg3, int arg4) {
		JFormattedTextField inputComponent = (JFormattedTextField) super.getTableCellEditorComponent(arg0, arg1, arg2, arg3, arg4);
		inputComponent.setValue(arg1);
		inputComponent.selectAll();
		return inputComponent;
	}
}
